
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package meeple;

/**
 *
 * @author rogier
 */
public class Tile implements BasicTile, RoadTile {
    private String     Name    = "";
    private Player     owner   = null;
    private int        turns   = 0;
    private TileType[] type    = new TileType[5];
    private Tile[]     tiles   = new Tile[4];
    private boolean    visited = false;
    private Position   meeple  = Position.none;

    Tile(Tile tile) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    Tile(String string, TileType[] tileType, int i, String string0) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    // <editor-fold defaultstate="collapsed" desc="/*comment*/">
    public void Rotate()    // </editor-fold>
    {                       // rotate tile once 90deg clocke-wise
        TileType tmp = type[type.length - 2];

        for (int i = type.length - 2; i > 0; i--) {
            type[i] = type[i - 1];
        }

        type[0] = tmp;
        turns   = (turns + 1) % 4;
    }

    public boolean isRoad() {
        int roads = 0;

        for (TileType t : type) {
            if (t == t.road) {
                roads++;
            }
        }

        if (roads == 2) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isJunction() {
        if (type[4] == TileType.junction) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isEnd() {
        int roads = 0;

        for (TileType t : type) {
            if (t == t.road) {
                roads++;
            }
        }

        if (roads == 1) {
            return true;
        } else {
            return false;
        }
    }

    public Tile getDirection(int dir)    // return reference to neigbour road tile
    {

        // dir can be -1,1,2,(3,4) where -1 means  the direction where the meeple is placed , 1 one directio 2, the opposite
        int roads = 0;
        int index = 0;

        if (dir == -1) {
            return tiles[getMeeple().arrayIndex()];
        }

        for (TileType t : type) {
            if (t == t.road) {
                roads++;
            }

            if (roads == dir) {
                return tiles[index];
            }

            index++;
        }

        return null;
    }

    /**
     * @param type the type to set
     */
    public void setType(TileType[] type) {
        if (this.type.length == type.length) {
            int x = 0;

            for (TileType i : type) {
                this.type[x] = i;
                x++;
            }
        }
    }

    /**
     * @return the top
     */
    public TileType getTop() {
        return this.type[0];
    }

    /**
     * @return the right
     */
    public TileType getRight() {
        return this.type[1];
    }

    /**
     * @return the bottom
     */
    public TileType getBottom() {
        return this.type[2];
    }

    /**
     * @return the left
     */
    public TileType getLeft() {
        return this.type[3];
    }

    public TileType getCenter() {
        return this.type[4];
    }

    /**
     * @return the visited
     */
    public boolean isVisited() {
        return visited;
    }

    /**
     * @param visited the visited to set
     */
    public void setVisited(boolean visited) {
        this.visited = visited;
    }

    /**
     * @return the topTile
     */
    public Tile getTopTile() {
        return tiles[0];
    }

    /**
     * @param topTile the topTile to set
     */
    public void setTopTile(Tile topTile) {
        if ((topTile != null) && (getTop() == topTile.getBottom())) {
            tiles[0] = topTile;
        }
    }

    /**
     * @return the rightTile
     */
    public Tile getRightTile() {
        return tiles[1];
    }

    /**
     * @param rightTile the rightTile to set
     */
    public void setRightTile(Tile rightTile) {
        if ((rightTile != null) && (getRight() == rightTile.getLeft())) {
            tiles[1] = rightTile;
        }
    }

    /**
     * @return the bottomTile
     */
    public Tile getBottomTile() {
        return tiles[2];
    }

    /**
     * @param bottomTile the bottomTile to set
     */
    public void setBottomTile(Tile bottomTile) {
        if ((bottomTile != null) && (getBottom() == bottomTile.getTop())) {
            tiles[2] = bottomTile;
        }
    }

    /**
     * @return the leftTile
     */
    public Tile getLeftTile() {
        return tiles[3];
    }

    /**
     * @param leftTile the leftTile to set
     */
    public void setLeftTile(Tile leftTile) {
        if ((leftTile != null) && (getLeft() == leftTile.getRight())) {
            tiles[3] = leftTile;
        }
    }

    /**
     * @return the meeple
     */
    public Position getMeeple() {
        return meeple;
    }

    /**
     * @param meeple the meeple to set
     */
    public void setMeeple(Position pos) {
        this.meeple = pos;
    }

    /**
     * @return the Name
     */
    public String getName() {
        return Name;
    }

    /**
     * @param Name the Name to set
     */
    public void setName(String Name) {
        this.Name = Name;
    }

    /**
     * @return the owner
     */
    public Player getOwner() {
        return owner;
    }

    /**
     * @param owner the owner to set
     */
    public void setOwner(Player owner) {
        this.owner = owner;
    }

    int getTurns() {
        return turns;
    }

    public TileType getTypes(Position p) {
        return type[meeple.arrayIndex()];
    }

    int getNumber() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
